<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

use App\Ad;

class AdController extends Controller
{
    //定义查询数组
    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'创建时间'],
        ['id'=>3,'searchtype'=>'更新时间'],
        ['id'=>4,'searchtype'=>'广告文字'],
        ['id'=>5,'searchtype'=>'广告图片'],
    ];

    //广告列表载入
    public function index() {
        $search = input::get('searchtype');
        $param = input::get('param');
        $obj = new Ad;
        if($search == 1) {
            $obj = $obj->where('id','like',"%$param%");
        }elseif($search == 2) {
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif($search == 3) {
            $obj = $obj->where('update_time','like',"%$param%");
        }elseif($search == 4) {
            $obj = $obj->where('text','like',"%$param%");
        }elseif($search == 5) {
            $obj = $obj->where('picture','like',"%$param%");
        }

        $list = $obj->all()->toArray();
        if($list == false && !empty($search)) {
            return  redirect()->back()->withInput()->withErrors('未查询到结果！');
        }
        $searchtype = self::$field;
        return view('admin.ad.index',['list'=>$list,'searchtype'=>$searchtype]);
    }

    //获取要修改的广告id
    public function getAd($id) {
        $list = Ad::find($id)->toArray();
        return view('admin.ad.updateAd',['list'=>$list]);
    }

    //更新广告
    public function updateAd() {
        $id = input::get('id');
        $data['text'] = input::get('text');
        $picture = input::file('picture');

        if(empty($data['text'])) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }
        if(!empty($picture)) {
            if($picture->isValid()) {
                $allowed_extensions = ['png','jpg','gif'];
                if($picture->getClientOriginalExtension() && !in_array($picture->getClientOriginalExtension(),$allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg or gif.'];
                }
                $destinationPath = 'productImg/'.date('Y-m-d');
                $extension = $picture->getClientOriginalExtension();
                $pictureName = str_random(10). '.' . $extension;
                $picture->move($destinationPath,$pictureName);
                $picturePath = asset($destinationPath.$pictureName);
                $data['picture'] = date('Y-m-d'). '/' . $pictureName;
            }
            else {
                return redirect()->back()->withInput()->withErrors('文件上传失败!');
            }
        }
        $list = Ad::where('id',$id)->update($data);
        if($list) {
            return Redirect::to('admin/ad')->withErrors('更新成功!');
        }
        else {
            return redirect()->back()->withInput()->withErrors('更新失败!');
        }
    }
}
